var myGoodsIndexCtrl  = new XFApp.client();

XFApp.client.prototype.myGoods = {
		successCode : 10000,	
		goodsPage : null,
		sortType:-1,//-1：时间倒序，1：正序
		loading:false,
		isSort:false,
		batchDialog:null,
		errorDialog :null,
		urlItems : {
			queryPageURL: basePath+"/userCenter/goods/queryPage",
			deleteURL:basePath+"/userCenter/goods/deleteGoods",
			submitBatch:basePath+"/userCenter/goods/submitGoodsBatch"
		},
		queryPage:function(){
			var _this = this;
			if(_this.loading){
				return false;
			}else{
				_this.loading = true;
			}
			var params = {};//$('#searchForm').serialize().replace(/\+/g, "%20");
			$("#searchForm input").each(function(){
				if(!!$(this).attr("name")){
					params[$(this).attr("name")] = $(this).val();
				}
			});
			params["sortType"]=_this.sortType;
			if(!!_this.goodsPage){
				_this.goodsPage.page("remote",0,params);
			}else{
				_this.goodsPage = $("#page").page({
				pageSize : 10,
				remote: {
					url: _this.urlItems.queryPageURL,
					params: params, 
			        success: function (data, pageIndex) {
			        	if(data.code==_this.successCode){
			        		if(null == data.result 
									|| null == data.result.itemList
										|| data.result.itemList.length < 1){
			        			$("#page_grid").html('<tr><td align="center" colspan="99">暂无数据</td></tr>');
			        		}else{			        			
			        			$("#page_grid").html($("#pageTmp").render(data.result.itemList));
			        		}
			        		if(_this.isSort){
			        			_this.isSort = false;
			        			if(_this.sortType==1 ){
									$("#icon_sort").removeClass("zi-down");
									$("#icon_sort").addClass("zi-up");
				        		}else{
				        			$("#icon_sort").removeClass("zi-up");
									$("#icon_sort").addClass("zi-down");
				        		}
			        		}
			        	}else{
			        		  window.webDialogUtil.alert("提示",data.msg);
			        	}
			        },
			        error : function(){
			        	window.webDialogUtil.alert("提示","商品查询异常");
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页,{total}项'
			});	
		   }
			_this.loading = false;
			return true;
		},
		
		submitBatch:function(){
			var _this = this;
			$.ajaxFileUpload({
				url :_this.urlItems.submitBatch,
				secureuri:false,
				fileElementId:'goodsFile',//file标签的id
				dataType: 'json',
				success: function (data, status) {
					if(!!_this.batchDialog){
						_this.batchDialog.close();
					}
					if(data.code!=10000){
						webDialogUtil.simpleAlert(data.msg, "温馨提示");
					}else{
						webDialogUtil.simpleAlert("导入成功", "温馨提示");
						if(!!_this.batchDialog){
							_this.batchDialog.close();
						}
						_this.queryPage();
					}
				},
				error: function (data, status, e) {
					if(!_this.errorDialog){
						_this.errorDialog = webDialogUtil.simpleAlert("处理异常", "温馨提示");
					}
					if(!!_this.batchDialog){
						_this.batchDialog.close();
					}
				}
			});
		},
		deleteGoods:function(docketCode){
			var _this = this;
			webDialogUtil.simpleConfrimAlert("是否确认删除?", function(){
				var params = {docketCode:docketCode};
				$.ajax({
					url : _this.urlItems.deleteURL,
					dataType : 'json',
					data  : params,
					type : 'post',
					success : function(data) {
						if(data.code==_this.successCode){
							webDialogUtil.simpleAlert("处理成功", "温馨提示");
							_this.queryPage();
						}else{
							webDialogUtil.simpleAlert(data.msg, "温馨提示");
						}
					},
					error:function(){
						webDialogUtil.simpleAlert("删除数据出现异常", "温馨提示");
					}
				});
			}, null, "温馨提示");
		},
		
		event:function(){
			var _this = this;
			$("#search_btn").on("click",function(){
				_this.queryPage();
			});
			$("#docketstatus_sel").on("change",function(){
				$("#docketstatus").val($(this).val());
				_this.queryPage();
			});
			$("#time_sort").on("click",function(){
				_this.isSort = true;
				_this.sortType*=-1;
				_this.queryPage();
			});
			$("#upload_btn").on("click",function(){
				if(!$("#goodsFile").val()){
					webDialogUtil.simpleAlert("请选择文件", "温馨提示");
				}else{
					_this.submitBatch();
				}
			});
		    $('#js_daoru').click(function(){
		    	$("#box_fileName").text("");
	    		$("#sel_file").css("display","");
	    		$("#goodsFile").val("");
	    		_this.errorDialog = null;
		    	_this.batchDialog = webAlert({
                    title:'批量导入',
                    content:document.getElementById("js_daoru_box"),
                });
		    	 $("#goodsFile").on("change",function(){
				    	if(!!$(this).val()){
				    		$("#sel_file").css("display","none");
				    		$("#box_fileName").text($("#goodsFile").val());
				    	}else{
				    		$("#box_fileName").text("");
				    		$("#sel_file").css("display","");
				    	}
				    });
            });
		    if(!!$("#goodsFile").val()){
	    		$("#sel_file").css("display","none");
	    		$("#box_fileName").text($("#goodsFile").val());
	    	}else{
	    		$("#box_fileName").text("");
	    		$("#sel_file").css("display","");
	    	}
		}
}
$.views.converters({
	fgoodsStatus : function(value) {
		if(!value){
			return "";
		}
		var result = goodsConstants.statusConts[value];
		if(!!result){
			if(value=="32"){
				result = "<span class='red'>"+result+"</span>";
			}
			if(value=="40"){
				result = "<span class='green'>"+result+"</span>";
			}
			return result;
		}
		return  "未知类型("+value+")";
	}
});
$(function(){
	myGoodsIndexCtrl.myGoods.event();
	myGoodsIndexCtrl.myGoods.queryPage();
});